package cn.est.service.impl;
import cn.est.constants.MathUtil;
import cn.est.mapper.SmsLogMapper;
import cn.est.pojo.SmsLog;
import cn.est.service.SmsLogService;
import cn.est.utils.RedisUtil;
import cn.est.utils.SMSUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import cn.est.dto.Page;
import cn.est.constants.Constants;
import org.springframework.transaction.annotation.Transactional;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;

@Service
@Transactional
public class SmsLogServiceImpl implements SmsLogService {

    @Resource
    private SmsLogMapper smsLogMapper;

    @Autowired
    private SMSUtil smsUtil;

    @Autowired
    private RedisUtil redisUtil;


    public SmsLog getSmsLogById(Long id){
        return smsLogMapper.getSmsLogById(id);
    }

    public List<SmsLog>	getSmsLogListByMap(Map<String,Object> param){
        return smsLogMapper.getSmsLogListByMap(param);
    }

    public Integer getSmsLogCountByMap(Map<String,Object> param){
        return smsLogMapper.getSmsLogCountByMap(param);
    }

    public Integer qdtxAddSmsLog(SmsLog smsLog){
            smsLog.setCreatdTime(new Date());
            return smsLogMapper.insertSmsLog(smsLog);
    }

    public Integer qdtxModifySmsLog(SmsLog smsLog){
        smsLog.setUpdatedTime(new Date());
        return smsLogMapper.updateSmsLog(smsLog);
    }

    public Integer qdtxDeleteSmsLogById(Long id){
        return smsLogMapper.deleteSmsLogById(id);
    }

    public Integer qdtxBatchDeleteSmsLog(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return smsLogMapper.batchDeleteSmsLog(param);
    }

    public Page<SmsLog> querySmsLogPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = smsLogMapper.getSmsLogCountByMap(param);
        pageNo = pageNo==null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize==null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<SmsLog> smsLogList = smsLogMapper.getSmsLogListByMap(param);
        page.setList(smsLogList);
        return page;
     }





    @Override
    public void sendMessage(String phone, Integer codeType) throws Exception {
        //1.调用短信接口发送短信信息
        String code = MathUtil.randomCode();
        SendSmsResponse  sendSmsResponse =smsUtil.sendSms(phone,"{code:"+code+"}");
        //2.保存短信的发送信息
        if(sendSmsResponse!=null&&sendSmsResponse.getBody().getCode().equals("OK")){
              //短信发送成功
            //3保存验证码并且保存有效期
            String key =Constants.Redis.PREFIX_SMS+codeType+":"+phone;

            //4.把验证码放在redis中、
            redisUtil.set(key,code,Constants.Duration.MINUTE_INT*5);

            SmsLog smsLog=new SmsLog();
            smsLog.setPhone(phone);
            smsLog.setCodeType(codeType);
            smsLog.setMessage(code);
            smsLog.setStatus(1);
            smsLog.setFailInfo("OK");
            smsLog.setCreatedUserId(Long.parseLong("0"));
            smsLog.setCreatdTime(new Date());
            smsLog.setUpdatedTime(new Date());
            smsLog.setIsDelete(0);

           try {
               smsLogMapper.insertSmsLog(smsLog);
           }catch (Exception e){
               e.printStackTrace();
           }

        }
    }


}
